
package com.monlyu.vo;

import java.io.File;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import org.apache.commons.io.FileUtils;

import com.monlyu.annotation.Comment;
import com.monlyu.util.DateUtil;
import com.monlyu.util.FileUtil;

/********************************************************************************
 * <b>功能描述☞</b>附件表<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.vo.Attachment.java<br>
 * <b>开发日期☞</b>2010-1-29 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 ********************************************************************************/
@Entity(name = "t_attach")
@Comment(common = "附件信息表")
public class Attachment extends BaseVo {

    @Id
    @Column(name = "a_id")
    @Comment(common = "ID编号")
    private Long id;

    @Column(name = "a_type", length = LENGTH_30, nullable = false)
    @Comment(common = "文件的MIME类型")
    private String type;

    @Column(name = "a_name", length = MAX_VCHAR_MYSQL, nullable = false)
    @Comment(common = "显示文件名")
    private String name;

    @Column(name = "a_path", length = MAX_VCHAR_MYSQL, nullable = false)
    @Comment(common = "文件本地存储路径")
    private String path;

    @Column(name = "a_size", length = LENGTH_30)
    @Comment(common = "文件大小")
    private long size;

    @Column(name = "a_local", length = LENGTH_50)
    @Comment(common = "本地存储文件名")
    private String localName;

    @ManyToOne
    @JoinColumn(name = "a_user", nullable = false)
    @Comment(common = "上传者")
    private User user;

    @Column(name = "a_date")
    @Comment(common = "上传时间")
    private Date date;

    @Column(name = "a_mark", length = BaseVo.MAX_VCHAR_MYSQL)
    private String mark;

    public String getMark(){
        return mark;
    }

    public void setMark(String mark){
        this.mark = mark;
    }

    public Long getId(){
        return id;
    }

    public void setId(Long id){
        this.id = id;
    }

    public String getType(){
        return type;
    }

    public void setType(String type){
        this.type = type;
    }

    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }

    public String getPath(){
        return path;
    }

    public void setPath(String path){
        this.path = path;
    }

    public String getLocalName(){
        return localName;
    }

    public void setLocalName(String localName){
        this.localName = localName;
    }

    public User getUser(){
        return user;
    }

    public void setUser(User user){
        this.user = user;
    }

    public Date getDate(){
        return date;
    }

    public long getSize(){
        return size;
    }

    public void setSize(long size){
        this.size = size;
    }

    public void setDate(Date date){
        this.date = date;
    }

    /**
     * 现实为字符串
     * 
     * @author monlyu
     * @since 2009-12-27
     */
    @Override
    public String toString(){
        return getSimpleName()
            + "("
            + DateUtil.format(getDate(), DateUtil.DATETIMENOS_PATTERN)
            + " | "
            + size2Mb()
            + ")";
    }

    /**
     * 获取简短的文件名
     * 
     * @author monlyu
     * @since 2010-1-30
     */
    private String getSimpleName(){
        String tmp = getName();
        int length = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tmp.length(); i++) {
            if (tmp.substring(i, i + 1).matches("[\\u4e00-\\u9fa5]+")) {
                length = length + 2;
            } else {
                length++;
            }
            sb.append(tmp.substring(i, i + 1));
            if (sb.length() >= 20) {
                sb.append("...");
                break;
            }
        }
        return sb.toString();
    }

    public String size2Mb(){
        return FileUtils.byteCountToDisplaySize(getSize());
    }

    public String file(){
        return path + File.separator + localName;
    }

    public String smallview(){
        return path + File.separator + FileUtil.appendFileName(localName, "_128");
    }
}
